package com.itheima.skyserver.controller;

import com.itheima.skyserver.pojo.domain.Employee;
import com.itheima.skyserver.pojo.dto.UpdatePwdDTO;
import com.itheima.skyserver.pojo.vo.EmpLoginVo;
import com.itheima.skyserver.result.PageResult;
import com.itheima.skyserver.result.Result;
import com.itheima.skyserver.service.EmployeeService;
import com.itheima.skyserver.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    /**
     * 编辑员工信息
     * @param employee
     * @return
     */
    @PutMapping()
    public Result updateEmployee(@RequestBody Employee employee) {
        employeeService.updateEmployee(employee);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result selectEmployeeById(@PathVariable Long id) {
        Employee employee = employeeService.selectEmployeeById(id);
        return Result.success(employee);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Employee employee) {
        // 1. 接收请求参数：JSON 格式
        log.info("员工登录：{}", employee);

        // 2. 调用 service 层，完成登录操作
        Employee loginEmp = employeeService.checkLogin(employee);

        // 3. 封装返回结果
        EmpLoginVo empLoginVo = new EmpLoginVo();
        BeanUtils.copyProperties(loginEmp, empLoginVo); // 在 tlias 中用到过

        Map claims = new HashMap<>();
        claims.put("id", loginEmp.getId());
        claims.put("username", loginEmp.getUsername());
        claims.put("name", loginEmp.getName());
        String token = JwtUtils.generateJwt(claims);
        empLoginVo.setToken(token);

        return Result.success(empLoginVo);
    }

    /**
     * 员工分页 + 多条件
     *
     * @param name
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result page(String name, Integer page, Integer pageSize) {
        // 1. 接收参数：kv
        log.info("员工分页查询：{}, {}, {}", name, page, pageSize);

        // 2. 调用 service 层，完成查询
        PageResult pageResult = employeeService.pageQuery(name, page, pageSize);

        // 3. 封装返回结果
        return Result.success(pageResult);
    }

    /**
     * 启用、禁用员工账号
     *
     * @param id 员工 id
     * @return
     */
    @PostMapping("/status/{status}")
    public Result setStatus(@PathVariable Integer status, Long id) {
        employeeService.setStatus(status, id);
        return Result.success();
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping()
    public Result insertEmployee(@RequestBody Employee employee) {
        employeeService.insertEmployee(employee);
        return Result.success();
    }

    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    @PutMapping("/editPassword")
    public Result editPassword(@RequestBody UpdatePwdDTO dto) {
        employeeService.editPassword(dto.getOldPassword(), dto.getNewPassword());
        return Result.success("修改成功");
    }
}
